import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:app_core/app_core.dart';
import 'app_base_page.dart';
import 'empty_state_example_logic.dart';
import '../widgets/empty_state_widget.dart';

/// 空状态示例页面
class EmptyStateExamplePage extends AppBasePage<EmptyStateExampleLogic> {
  EmptyStateExamplePage({super.key});

  @override
  final logic = Get.put(EmptyStateExampleLogic());

  @override
  String? get title => '空状态示例';

  @override
  Widget? get rightWidget => IconButton(icon: const Icon(Icons.refresh), onPressed: () => logic.onRefresh(), tooltip: '刷新数据');

  @override
  Widget buildContent(BuildContext context) {
    return wrapWithRefresh(child: SingleChildScrollView(padding: const EdgeInsets.all(16), child: _buildDataList()), onRefresh: logic.onRefresh);
  }

  /// 构建数据列表 - 在子类中处理空状态
  Widget _buildDataList() {
    return Obx(() {
      if (logic.dataList.isEmpty) {
        return const EmptyStateWidget.listEmpty(actionText: '添加数据', onAction: _addExampleData);
      }

      return Column(children: logic.dataList.map((item) => _buildListItem(item)).toList());
    });
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text('数据列表', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            const SizedBox(height: 12),
            Obx(() {
              // 在子类中判断是否为空，并显示对应的空状态
              if (logic.dataList.isEmpty) {
                return const EmptyStateWidget.listEmpty(actionText: '添加数据', onAction: _addExampleData);
              }

              return Column(children: logic.dataList.map((item) => _buildListItem(item)).toList());
            }),
          ],
        ),
      ),
    );
  }

  /// 添加示例数据的静态方法（供空状态组件调用）
  static void _addExampleData() {
    final logic = Get.find<EmptyStateExampleLogic>();
    logic.addExampleData();
  }

  /// 构建列表项
  Widget _buildListItem(ExampleItem item) {
    return Container(
      margin: const EdgeInsets.only(bottom: 8),
      decoration: BoxDecoration(border: Border.all(color: Colors.grey.shade300), borderRadius: BorderRadius.circular(8)),
      child: ListTile(
        title: Text(item.title),
        subtitle: Text(item.description),
        trailing: IconButton(icon: const Icon(Icons.delete, color: Colors.red), onPressed: () => logic.deleteItem(item.id)),
      ),
    );
  }

  /// 构建操作按钮
  Widget _buildActionButtons() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text('操作按钮', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            const SizedBox(height: 12),
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: [
                ElevatedButton(onPressed: () => logic.loadData(), child: const Text('重新加载')),
                ElevatedButton(onPressed: () => logic.clearData(), style: ElevatedButton.styleFrom(backgroundColor: Colors.orange), child: const Text('清空数据')),
                ElevatedButton(onPressed: () => _showAddDialog(), style: ElevatedButton.styleFrom(backgroundColor: Colors.green), child: const Text('添加数据')),
              ],
            ),
          ],
        ),
      ),
    );
  }

  /// 显示添加对话框
  void _showAddDialog() {
    final titleController = TextEditingController();
    final descController = TextEditingController();

    Get.dialog(
      AlertDialog(
        title: const Text('添加数据'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextField(controller: titleController, decoration: const InputDecoration(labelText: '标题')),
            const SizedBox(height: 8),
            TextField(controller: descController, decoration: const InputDecoration(labelText: '描述')),
          ],
        ),
        actions: [
          TextButton(onPressed: () => Get.back(), child: const Text('取消')),
          ElevatedButton(
            onPressed: () {
              if (titleController.text.isNotEmpty) {
                final newItem = ExampleItem(
                  id: DateTime.now().millisecondsSinceEpoch.toString(),
                  title: titleController.text,
                  description: descController.text,
                );
                logic.dataList.add(newItem);
                Get.back();
                YHSmartDialog.showSuccessMsg('添加成功！');
              }
            },
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }
}
